<?php
/**
 * Created by PhpStorm.
 * User: alice
 * Date: 2015/12/28
 * Time: 12:26
 */

/**
 * Class Involved_OrderModel
 */

class Involved_OrderModel extends BaseModel{

    protected $table = 'order';





    /**
     * 获取订单列表
     * @param $user_id
     * @param $page
     * @return mixed
     * @author alice
     * @date 2016-9-8
     */
    public function APPOrderList($user_id,$status)
    {
        $db = DB::table($this->table);
        if($status==10) {//待审核
            $db->where(['user_id' =>$user_id, 'order_status' => 10]);
        }elseif($status==20) {//待付款
            $db->where(['user_id' =>$user_id, 'order_status' => 20]);
        }elseif($status==30) {//待提货
            $db->where(['user_id' =>$user_id, 'order_status' => 30]);
        }elseif($status==40) {//待开票
            $db->where(['user_id' =>$user_id, 'order_status' => 40]);
        }else{
            $db->where('user_id' ,$user_id);
            $db->where( 'order_status', '!=', 70);
        }
        return  $db
            ->select('order.order_id','order.payment_id','order.order_time','order.order_sn','order.user_id','order.amount','order_product.product_id','order.order_status',
                'product.product_spec','brand.brand_name','material.m_name','factory.factory_name','product.product_price','order_product.number')
            ->leftJoin('order_product','order.order_id','=','order_product.order_id')
            ->leftJoin('product','order_product.product_id','=','product.product_id')
            ->leftJoin('material', 'material.m_id', '=', 'product.product_material')
            ->leftJoin('brand', 'brand.brand_id', '=', 'product.product_brand')
            ->leftJoin('factory', 'factory.factory_id', '=', 'product.factory')
            ->orderBy('order_time','desc')
            ->get();

    }










    /**
     * 获取订单列表
     * @param $user_id
     * @param $page
     * @return mixed
     * @author alice
     * @date 2016-2-18
     */
    public function GetOrderList($user_id,$page)
    {
        $offset = ($page-1)* HostEnum::PAGE_NUM;

//        return  DB::table($this->table)
//                ->select('order.order_id','order.order_sn','order.order_time','order.amount','order.order_status',
//                        'order_product.product_id',
//                        'logistics_status.logistics_status')
//                ->LeftJoin('order_product','order.order_id','=','order_product.order_id')
//                ->LeftJoin('logistics_status','order.order_id','=','logistics_status.order_id')
//                ->groupBy('order_product.order_id')
//                ->where('user_id',$user_id)
//                ->skip($offset)->take(HostEnum::PAGE_NUM)
//                ->get();

        return  DB::table($this->table)
                ->select('order.order_id','order.order_sn','order.order_time','order.amount','order.order_status',
                        'logistics_status.log_status')
                ->LeftJoin('logistics_status','order.order_id','=','logistics_status.order_id')
                ->where('user_id',$user_id)
                ->skip($offset)->take(HostEnum::PAGE_NUM)
                ->get();


    }
    /**
     * 获取订单详情后台
     * @param $order_id
     * @return mixed
     * liuyuchao 09.08
     */
    public function GetOrderDetailAdmin($order_id)
    {
        return  DB::table($this->table)
            ->select('order.order_id','order.order_sn','order.order_status','trade_num','amount','order.order_time','order.user_id',
                'order_product.product_id','order_product.number','user.user_name','brand.brand_name','order.payment_id','order.message')
            ->LeftJoin('order_product','order.order_id','=','order_product.order_id')
            ->LeftJoin('user','order.user_id','=','user.user_id')
            ->LeftJoin('product','order_product.product_id','=','product.product_id')
            ->leftJoin('brand','product.product_brand','=','brand.brand_id')
            ->where('order.order_id',$order_id)
            ->first();


    }
    /**
     * 获取订单详情
     * @param $order_id
     * @return mixed
     * liuyuchao 09.08
     */
    public function GetOrderDetail($order_id)
    {
        return  DB::table($this->table)
            ->select('order.order_id','order.payment_id','order.order_time','order.order_sn','order.user_id','order.amount'
                ,'order_product.product_id','order.order_status', 'product.product_spec','product.product_sn','product_price','brand.brand_name'
                ,'material.m_name','factory.factory_name','order_product.number','storage.storage_name','storage.storage_address'
                ,'storage.storage_phone')
            ->leftJoin('order_product','order.order_id','=','order_product.order_id')
            ->leftJoin('product','order_product.product_id','=','product.product_id')
            ->leftJoin('material', 'material.m_id', '=', 'product.product_material')
            ->leftJoin('brand', 'brand.brand_id', '=', 'product.product_brand')
            ->leftJoin('factory', 'factory.factory_id', '=', 'product.factory')
            ->leftJoin('storage', 'storage.storage_id', '=', 'product.storage')
            ->where('order.order_id',$order_id)
            ->first();

    }


    /**
     * 获取订单信息
     * @param $order_id
     * @return mixed
     */
    public function ConfirmOrder($order_id)
    {
        return  DB::table($this->table)->where('order_id',$order_id)->get();
    }

    /**
     * 取消订单
     * @param $order_id
     * @param $user_id
     * @return mixed
     * @throws Exception
     */
    public function CancelOrder($order_id)
    {
        #是否我的订单
        $order_info = DB::table($this->table)->where(['order_id' => $order_id])->first();
        if(!$order_info)
            throw new Exception('订单不存在', ProfessionErrorCodeEnum::ERROR_DB_UPDATE);

        #是否可以取消订单
        if ($order_info->order_status != OrderEnum::STATUS_NO_LOCKING && $order_info->order_status != OrderEnum::STATUS_NO_PAY )
            throw new Exception('订单状态不正确', ProfessionErrorCodeEnum::ERROR_DB_UPDATE);

        return  DB::table($this->table)->where('order_id',$order_id)->update(array('order_status' => OrderEnum::STATUS_CANCEL));
    }
    /**
     * 确认收货
     * @param $order_id
     * @return mixed
     * @throws Exception
     */
    public function ConfirmReceipt($order_id)
    {
        #是否我的订单
        $order_info = DB::table($this->table)->where(['order_id' => $order_id])->first();
        if(!$order_info)
            throw new Exception('订单不存在', ProfessionErrorCodeEnum::ERROR_DB_UPDATE);

        #是否可以取消订单
        if ($order_info->order_status != OrderEnum::STATUS_PAYED)
            throw new Exception('订单状态不正确', ProfessionErrorCodeEnum::STATUS_PAYED);
        return  DB::table($this->table)->where('order_id',$order_id)->update(array('order_status' => OrderEnum::STATUS_TAKE_GOODS,'deal_time'=>time() ));
    }
    /**
     * 修改价格
     * @param $order_id
     * @return mixed
     * @throws Exception
     */
    public function ChangePrice($order_id,$amount)
    {
        #是否我的订单
        $order_info = DB::table($this->table)->where(['order_id' => $order_id])->first();
        if(!$order_info)
            throw new Exception('订单不存在', ProfessionErrorCodeEnum::ERROR_DB_UPDATE);

        return  DB::table($this->table)->where('order_id',$order_id)->update(array('amount' => $amount ));
    }

    /**
     * 确认发货
     * @param $order_id
     * @return mixed
     * @throws Exception
     */
    public function SendGoods($order_id)
    {
        #是否我的订单
        $order_info = DB::table($this->table)->where(['order_id' => $order_id])->first();
        if(!$order_info)
            throw new Exception('订单不存在', ProfessionErrorCodeEnum::ERROR_DB_UPDATE);

        return  DB::table($this->table)->where('order_id',$order_id)->update(array('order_status' => OrderEnum::STATUS_DELIVERY,'send_time'=>time() ));
    }
    /**
     * 删除订单
     * @param $order_id
     * @return mixed
     * @throws Exception
     */
    public function DeleteOrder($order_id)
    {
//        $order_info = $this->ConfirmOrder($order_id);
//        if ($order_info->order_status != OrderEnum::STATUS_RECEIVE || )
//            throw new Exception('订单状态不正确', ProfessionErrorCodeEnum::ERROR_DB_UPDATE);

        return  DB::table($this->table)->where('order_id',$order_id)->update(array('order_status' => OrderEnum::STATUS_DELETE));
    }


    public function GetOrderCount($user_id,$status){
        if($status == 0){
            return  DB::table($this->table)
                ->select('order_id')
                ->where('user_id',$user_id)
                ->where('order_status', '!=', 7)
                ->get();
        }elseif($status == 1){
            return  DB::table($this->table)
                ->select('order_id')
                ->where('user_id',$user_id)
                ->where('order_status', '=', 1)
                ->get();

        }else{
            return  DB::table($this->table)
                ->select('order_id')
                ->where('user_id',$user_id)
                ->where(function($query){
                    $query->where('order_status','=' ,2)
                        ->orWhere(function($query){
                            $query->where('order_status','=' ,4);
                        });
                })
                ->get();

        }

    }

    /**
     * Created by PhpStorm.
     * PC我的订单（全部，待付款，待收货）
     * User: liuyuchao
     * Date: 2016/09/08
     * Time: 14:06
     */
    public function OrderSelectPc($user_id,$status)
    {
        $db = DB::table($this->table);
        //品名
        if($status==10) {//待审核
            $db->where(['user_id' =>$user_id, 'order_status' => 10]);
        }elseif($status==20) {//待付款
            $db->where(['user_id' =>$user_id, 'order_status' => 20]);
        }elseif($status==30) {//待提货
            $db->where(['user_id' =>$user_id, 'order_status' => 30]);
        }elseif($status==40) {//待开票
            $db->where(['user_id' =>$user_id, 'order_status' => 40]);
        }else{
            $db->where('user_id' ,$user_id);
            $db->where( 'order_status', '!=', 70);
        }
        return $db
            ->select('order.order_id','order.payment_id','order.order_time','order.order_sn','order.user_id','order.amount','order_product.product_id','order.order_status',
                'product.product_spec','brand.brand_name','material.m_name','factory.factory_name')
            ->leftJoin('order_product','order.order_id','=','order_product.order_id')
            ->leftJoin('product','order_product.product_id','=','product.product_id')
            ->leftJoin('material', 'material.m_id', '=', 'product.product_material')
            ->leftJoin('brand', 'brand.brand_id', '=', 'product.product_brand')
            ->leftJoin('factory', 'factory.factory_id', '=', 'product.factory')
            ->orderBy('order_time','desc')
            ->get();

        }


//更改状态
    public function updatestatus($TradeNum){
        return DB::table($this->table)->where(['trade_num' => $TradeNum])->update(array('order_status' => OrderEnum:: STATUS_PAYED,'pay_time'=>time() ));


    }
    public function UpdateTradeNum($TradeNum){
        $num = 'MTW'.time().rand(100,999);
        return DB::table($this->table)->where(['trade_num' => $TradeNum])->update(array('trade_num' =>$num));


    }
//搜索订单字段提交给支付
    public function SelectOrder($order_id){
        return  DB::table($this->table)
            ->select('order_id','order_sn','order_time','amount','trade_num')
            ->where('order_id',$order_id)
            ->first();
    }
    //POS机订单
    public function posorderinfo($order_id){
        return  DB::table($this->table)
            ->select('order_id','order_sn','order_time','amount','trade_num')
            ->where('order_id',$order_id)
            ->first();
    }
    public function OrderList()
    {
        return DB::table($this->table)
            ->select('user_name','order_sn','order.amount','order_time','order_status','payment_id','order.order_id')
            ->leftjoin('user','order.user_id','=','user.user_id')
           // ->leftjoin('order_pay','order.payment_id','=','order_pay.pay_id')
           ->orderBy('order.order_id','desc')
            ->get();
    }
    //订单超时
    public function Order_Expired(){
        $status = 1;
        return  DB::table($this->table)
            ->select('order_id','order_time')
            ->where('order_status',$status)
            ->get();

    }

    /**
     * 删除订单PC
     * @param $order_id
     * @return mixed
     * @throws Exception
     */
    public function DeleteOrderPc($order_id)
    {

        return  DB::table($this->table)->where('order_id',$order_id)->update(array('order_status' => OrderEnum::STATUS_DELETE));
    }



    /**
     * APP取消订单
     * @param $order_id
     * @param $user_id
     * @return mixed
     * @throws Exception
     */
    public function APPCancelOrder($order_id,$user_id)
    {
        return  DB::table($this->table)
            ->select('order.order_id','order.order_status','product.product_id')
            ->leftJoin('order_product','order.order_id','=','order_product.order_id')
            ->leftJoin('product','order_product.product_id','=','product.product_id')
            ->where('order.order_id',$order_id)
            ->where('order.user_id',$user_id)
            ->where('order.order_status','<=','60')
            ->first();
        return  $order_info;
    }



    /**
     * APP获取订单详情
     * @param $user_id
     * @param $page
     * @return mixed
     * @author alice
     * @date 2016-9-9
     */
    public function APPOrderDetail($order_id)
    {
        return  DB::table($this->table)
            ->select('order.order_id','order.payment_id','order.order_time','order.order_sn','order.user_id','order.amount','order_product.product_id','order.order_status','product.piece_weight',
                'product.product_spec','brand.brand_name','material.m_name','factory.factory_name','storage.storage_name','storage.storage_city','storage.storage_phone','product.product_price','order_product.number','storage.storage_address','order.pay_time'
            )
            ->leftJoin('order_product','order.order_id','=','order_product.order_id')
            ->leftJoin('product','order_product.product_id','=','product.product_id')
            ->leftJoin('material', 'material.m_id', '=', 'product.product_material')
            ->leftJoin('brand', 'brand.brand_id', '=', 'product.product_brand')
            ->leftJoin('factory', 'factory.factory_id', '=', 'product.factory')
            ->leftjoin('storage','product.storage', '=', 'storage.storage_id')
            ->where('order.order_id',$order_id)
            ->first();


    }
    /**
     * 发票查询
     * 09。13
     */
    public function SelectInvoice($paramObj){
        $db = DB::table($this->table);
        if(!empty($paramObj->start_time) && $paramObj->end_time) {//开始时间
            $db->whereBetween('order_time', array($paramObj->start_time, $paramObj->end_time));
        }elseif(!empty($paramObj->title)  ){
            $db->where('invoice.title','=' ,$paramObj->title);
        }if(!empty($paramObj->status)){
            $db->where('invoice.status','=' ,$paramObj->status);
        }
        return $db
            ->select('order.order_id','order.order_time','order.order_sn','order.user_id',
                'order.order_status','amount','invoice.title','invoice.created','invoice.status')
            ->leftjoin('invoice','order.order_id','=','invoice.order_id')
            ->where('order.user_id',$paramObj->user_id)
            ->where(function($query){
                $query->where('order_status','=' ,40)
                    ->orWhere(function($query){
                        $query->where('order_status','=' ,50);
                    });
            })
            ->get();

    }

    /**
     * @param $user_id
     * 搜索公司抬头
     */
    public function SelectTitle($user_id){
        $db = DB::table($this->table);
        return $db
            ->select('invoice.title')
            ->leftJoin('invoice','order.order_id','=','invoice.order_id')
            ->where('order.user_id',$user_id)
            ->distinct()
            ->get();

    }


    /**
     * 搜索订单字段提交给支付
     * @param $order_id
     * @param $user_id
     * @return mixed
     * @throws Exception
     */
    public function APPSelectOrder($order_id){
        return  DB::table($this->table)
            ->select('order_id','order_sn','order_time','amount','trade_num')
            ->where('order_id',$order_id)
            ->first();
    }
    /**
     * 更改状态
     * @param $order_id
     * @return mixed
     * @throws Exception
     */
    public function APPUpStatus($TradeNum){
        return DB::table($this->table)->where(['trade_num' => $TradeNum])->update(array('order_status' =>30,'pay_time'=>time()));


    }
    /**
     *
     * 根据流水号查询订单ID
     */
    public function SelectOrderId($trade_num){
        return  DB::table($this->table)
            ->select('order_id')
            ->where('trade_num',$trade_num)
            ->first();

    }

}